package com.ruoyi.project.wechatprogram.weekend.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ch.qos.logback.core.encoder.EchoEncoder;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.wechatprogram.weekend.controller.WxWeekendController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.wechatprogram.weekend.mapper.WxWeekendMapper;
import com.ruoyi.project.wechatprogram.weekend.domain.WxWeekend;
import com.ruoyi.project.wechatprogram.weekend.service.IWxWeekendService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 餐品周期菜单Service业务层处理
 * 
 * @author csx
 * @date 2020-04-08
 */
@Service
public class WxWeekendServiceImpl implements IWxWeekendService 
{

    private static final Logger log = LoggerFactory.getLogger(WxWeekendServiceImpl.class);

    @Autowired
    private WxWeekendMapper wxWeekendMapper;

    /**
     * 查询餐品周期菜单
     * 
     * @param id 餐品周期菜单ID
     * @return 餐品周期菜单
     */
    @Override
    public WxWeekend selectWxWeekendById(Integer id)
    {
        return wxWeekendMapper.selectWxWeekendById(id);
    }

    /**
     * 查询餐品周期菜单列表
     * 
     * @param wxWeekend 餐品周期菜单
     * @return 餐品周期菜单
     */
    @Override
    public List<WxWeekend> selectWxWeekendList(WxWeekend wxWeekend)
    {
        return wxWeekendMapper.selectWxWeekendList(wxWeekend);
    }

    /**
     * 查询餐品周期菜单列表，查询当日以点餐的餐品
     *
     * @param wxWeekend 餐品周期菜单
     * @return 餐品周期菜单
     */
    @Override
    public List<WxWeekend> selectWxWeekendListByToday(WxWeekend wxWeekend)
    {
        return wxWeekendMapper.selectWxWeekendListByToday(wxWeekend);
    }

    /**
     * 新增餐品周期菜单
     * 
     * @param wxWeekend 餐品周期菜单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWxWeekend(WxWeekend wxWeekend)
    {
        wxWeekend.setCreateTime(DateUtils.getNowDate());
        return wxWeekendMapper.insertWxWeekend(wxWeekend);
    }

    /**
     * 添加餐品周期菜单信息
     *
     * @param weekIds
     * @param weekendName
     * @param categoryIds
     * @param menuIds
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveWeekend(Integer weekIds, String weekendName, Integer categoryIds, String menuIds)
    {
        try {
            // 清空当前选中的日期和类目
            wxWeekendMapper.delWxWeekByWeekIdAndCateId(weekIds, categoryIds);
            // 保存餐品周期菜单
            WxWeekend wxWeekend = null;
            String[] menuIdStr = menuIds.replace("，", ",").split(",");
            // 开始批量保存餐品周期菜单
            List<WxWeekend> weekendList = new ArrayList<>();
            for (String str : menuIdStr) {
                // 保存餐品周期菜单
                wxWeekend = new WxWeekend();
                wxWeekend.setWeekendId(weekIds);
                wxWeekend.setWeekendName(weekendName);
                wxWeekend.setCategoryId(categoryIds);
                wxWeekend.setMenuId(Integer.parseInt(str));
                wxWeekend.setCreateBy(ShiroUtils.getLoginName());
                wxWeekend.setCreateTime(new Date());
                weekendList.add(wxWeekend);
            }
            Integer successStatus = wxWeekendMapper.insertBatchWxWeekend(weekendList);
            if (successStatus <= 0) {
                log.error("插入失败，数据为：" + wxWeekend.toString());
                return AjaxResult.error("添加菜品失败！");
            }
        } catch (Exception e) {
            log.error("添加错误！", e);
            throw new BusinessException("添加错误！");
        }
        return AjaxResult.success();
    }

    /**
     * 修改餐品周期菜单
     * 
     * @param wxWeekend 餐品周期菜单
     * @return 结果
     */
    @Override
    public int updateWxWeekend(WxWeekend wxWeekend)
    {
        wxWeekend.setUpdateTime(DateUtils.getNowDate());
        return wxWeekendMapper.updateWxWeekend(wxWeekend);
    }

    /**
     * 删除餐品周期菜单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWxWeekendByIds(String ids)
    {
        return wxWeekendMapper.deleteWxWeekendByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除餐品周期菜单信息
     * 
     * @param id 餐品周期菜单ID
     * @return 结果
     */
    @Override
    public int deleteWxWeekendById(Integer id)
    {
        return wxWeekendMapper.deleteWxWeekendById(id);
    }
}
